const path = require('path');
const { VueLoaderPlugin } = require('vue-loader');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const webpack = require('webpack')
const env = require('./env')
const { NODE_ENV } = env

const assetsPublicPath = NODE_ENV === 'production' ? '/public' : '/'

module.exports = {
    mode: NODE_ENV,
    entry: {
        app: './src/main.js'
    },
    output: {
        filename: '[name].[contenthash:8].js',
        path: path.resolve(__dirname, './dist'),
        publicPath: assetsPublicPath
    },
    resolve: {
        alias: {
            '@': path.join(__dirname, './src'),
        }
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                loader: 'vue-loader',                
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
            },
            {
                enforce: 'pre',
                test: /\.(js|vue)$/,
                loader: 'eslint-loader',
                exclude: /node_modules/
            },
            {
                test: /\.css$/,
                loader: [MiniCssExtractPlugin.loader, 'css-loader'],           
            },
            {
                test: /\.less$/,
                loader: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'],
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10,
                    name: 'static/images/[name].[hash:7].[ext]',
                    esModule: false,
                }
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                loader: 'url-loader',
                options: {
                    limit: 10000,
                    name: 'static/fonts/[name].[hash:7].[ext]',
                    esModule: false,
                }
            },
        ]
    },
    plugins: [
        new webpack.DefinePlugin({
            'process.env': {
                ...env,
                NODE_ENV: `"${env.NODE_ENV}"`, // fix development is not defined error
            }
        }),
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            title: env.TITLE || 'vue web app',
            template: 'public/index.html',
            templateParameters: {
                BASE_URL: assetsPublicPath
            },
            inject: true,
        }),
        new MiniCssExtractPlugin({
            filename: '[name].[contenthash:8].css',           
        }),
    ],
}